/**
    ATClab - scriptable Air Traffic Control simulations.
    Copyright (C) 2010 openatclab@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

#include "task.h"

#include "atc.h"
#include <sstream>
#include <qtimer.h>
//Added by qt3to4:
#include <QShowEvent>
#include <QEvent>
#include <QHideEvent>
#include <QMouseEvent>
#include <QKeyEvent>

using namespace pact;

//------------------------------------------------------------------------------
// Construction/Destruction
//

/*!
 *
 */
Task::Task()
	  : QWidget()
	  , _time()
	  , _accel(this)
{}

/*!
 *
 */
Task::~Task() {}


//------------------------------------------------------------------------------
// [public] interface
//

/*!
 * Add a task termination key
 */
void Task::addTerminationKey( const QKeySequence &key ) {
	_accel.connectItem( _accel.insertItem(key), this, SLOT(terminate()) );
}

/*!
 * Add a task termination key
 */
void Task::addTerminationKey( const QString &key ) {
	addTerminationKey( QKeySequence( key ) );
}


/*!
 *
 */
void Task::addTerminationTime( int secs ) {
	QTimer::singleShot( secs * atc::ONE_SECOND, this, SLOT(terminate()) );
}

//--------------------------------------------------------------------------------------------------
// [public] slots
//

/*!
 *
 */
void Task::terminate() {
	logging::KVPairs kvs;
	kvs.push_back(std::make_pair( std::string("elapsed"), QString("%1").arg(_time.elapsed()).latin1() ));
	kvs.push_back(std::make_pair(std::string("action"), std::string("terminated") ));
	logging::Logger::instance()->info( kvs );

	QTimer::singleShot( 0, this, SIGNAL( closed() ) );
}

/*!
 *
 */
void Task::terminate( const char *msg ) {
	QTimer::singleShot( 0, this, SIGNAL( closed() ) );
}

/*!
 *
 */
void Task::timeout() {
	logging::KVPairs kvs;
	kvs.push_back(std::make_pair( 
			std::string("elapsed"), 
			QString("%1").arg(_time.elapsed()).latin1() 
		));
	kvs.push_back(std::make_pair(
			std::string("action"), std::string("timed-out") 
		));
	logging::Logger::instance()->info( kvs );

	QTimer::singleShot( 0, this, SIGNAL( closed() ) );
}

/*!
 *
 */
void Task::log_data( logging::KVPairs &data ) {
	data.insert( 
			data.begin(),  
			std::make_pair( std::string("elapsed"), QString("%1").arg(time()).latin1() ) 
		);
	logging::Logger::instance()->info(data);
}


//------------------------------------------------------------------------------
// [protected] virtual
//


/*!
 *
 */
void Task::hideEvent( QHideEvent * ) {
	releaseMouse();
	releaseKeyboard();
}

/*!
 *
 */
void Task::keyPressEvent( QKeyEvent *e ) {
	e->ignore();
}

/*!
 *
 */
void Task::mouseDoubleClickEvent( QMouseEvent* e ) {
	log_mouse( e );
}

/*!
 *
 */
void Task::mouseMoveEvent( QMouseEvent* e ) {
	log_mouse( e );
}

/*!
 *
 */
void Task::mousePressEvent( QMouseEvent* e ) {
	log_mouse( e );
}

/*!
 *
 */
void Task::mouseReleaseEvent( QMouseEvent* e ) {
	log_mouse( e );
}

/*!
 *
 */
void Task::showEvent(QShowEvent * ) {
	_time.start();
}


//------------------------------------------------------------------------------
// [private] implementation
//

/*!
 *
 */
void Task::log_mouse( QMouseEvent* e ) {
	std::string type =
		e->type() == QEvent::MouseButtonDblClick ? "dbl_clicked" :
		e->type() == QEvent::MouseMove           ? "moved"       :
		e->type() == QEvent::MouseButtonPress    ? "pressed"     :
		e->type() == QEvent::MouseButtonRelease  ? "released"    :
		                                           "UNKOWN"      ;
		
	logging::KVPairs kvs;
	kvs.push_back(std::make_pair(std::string("elapsed"), QString("%1").arg(_time.elapsed()).latin1()));
	kvs.push_back(std::make_pair(std::string("mouse"), type));
	kvs.push_back(std::make_pair(std::string("x"), QString("%1").arg(e->x()).latin1()));
	kvs.push_back(std::make_pair(std::string("y"), QString("%1").arg(e->y()).latin1()));
	kvs.push_back(std::make_pair(std::string("button"), QString("%1").arg(e->button()).latin1()));
	kvs.push_back(std::make_pair(std::string("state"), QString("%1").arg(e->state()).latin1()));
	logging::Logger::instance()->info(kvs);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
